﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace wpfTool
{
    /// <summary>
    /// Interaction logic for PropertyGrid.xaml
    /// </summary>
    public partial class PropertyGrid : UserControl
    {
        ShaderBase shader;

        SortedDictionary<int, string> gridItemDict = null;

        // should never be used!
        public PropertyGrid()
        {
            InitializeComponent();

            throw new System.Exception("Wrong Ctor!");
        }

        public PropertyGrid(ShaderBase sdr)
        {
            InitializeComponent();

            shader = sdr;
            gridItemDict = new SortedDictionary<int, string>();

            setupGrid();
        }

        void setupGrid()
        {
            GridLabel.Content = "  " + shader.ShaderName;
            Grid.SetRowSpan(grayRectV, shader.PropertyItemsList.Length + 1);
            Grid.SetRowSpan(theSplitter, shader.PropertyItemsList.Length + 1);

            RowDefinitionCollection rds = theGrid.RowDefinitions;
            // build grids and its boards
            for (int i = 0; i < shader.PropertyItemsList.Length; ++i)
            {
                RowDefinition rd = new RowDefinition();
                rd.Height = new GridLength(20);
                rds.Add(rd);

                // board
                Border bdr1 = new Border();
                bdr1.BorderThickness = new Thickness(1);
                bdr1.BorderBrush = Brushes.LightGray;
                Grid.SetRow(bdr1, i + 1);
                Grid.SetColumn(bdr1, 1);
                theGrid.Children.Add(bdr1);

                Border bdr3 = new Border();
                bdr3.BorderThickness = new Thickness(1);
                bdr3.BorderBrush = Brushes.LightGray;
                Grid.SetRow(bdr3, i + 1);
                Grid.SetColumn(bdr3, 3);
                theGrid.Children.Add(bdr3);
                
            }

            int row = 0;
            // add frameworkelements to grid
            foreach (PropertyInfo pi in shader.PropertyItemsList)
            {
                ++row;

                Label l = new Label();
                l.Padding = new Thickness(2);
                l.Content = pi.propertyName;
                Grid.SetRow(l, row);
                Grid.SetColumn(l, 1);
                theGrid.Children.Add(l);

                FrameworkElement fe = pi.element;
                Grid.SetRow(fe, row);
                Grid.SetColumn(fe, 3);
                theGrid.Children.Add(fe);
                (fe as IPropertyGridItem).registerItemUpdateEvent(this);
                (fe as IPropertyGridItem).setID(row);
                gridItemDict[(fe as IPropertyGridItem).getID()] = pi.propertyName;
            }
        }

        internal void OnGridItemChanged(FrameworkElement fe, object value)
        {
            // update the value in shader
            shader.PropertyList[gridItemDict[(fe as IPropertyGridItem).getID()]] = value;

            // issuse event to update the whole pipline
            ShaderPropertyChange(shader);
        }

        public event Window1.ShaderPropertyChangeHandler ShaderPropertyChange;
    }

    internal class ScrollViewerConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int num = int.Parse(value.ToString());

            return num - 60;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}
